package cn.sdormitory.controller.workflow;


import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.common.exception.ApiException;
import cn.sdormitory.workflow.entity.ActReProcdef;
import cn.sdormitory.workflow.service.WorkFlowCommonService;
import cn.sdormitory.workflow.vo.WorkFlowHiVO;
import cn.sdormitory.workflow.vo.WorkFlowVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.repository.Deployment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;

/**
 * 工作流常用接口公共类
 */
@RestController
@RequestMapping("/workflow")
@Slf4j
public class WorkFlowCommonController {

    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    private WorkFlowCommonService workFlowCommonService;

    /**
     * 上传部署流程
     * @param file
     * @return
     * @throws ApiException
     */
    @PostMapping("/uploadProcessZip")
    public CommonResult<Integer> uploadProcess(@RequestParam("file") MultipartFile file) throws ApiException, IOException {
        String fileName = file.getOriginalFilename();
        String fileTyle = fileName.substring(fileName.lastIndexOf("."), fileName.length());
        if (!".zip".equalsIgnoreCase(fileTyle)) {
            return CommonResult.failed("请上传zip文件");
        }
        InputStream in = file.getInputStream();
        ZipInputStream zipInputStream = new ZipInputStream(in);
        Deployment deployment = processEngine.getRepositoryService()//获取流程定义和部署对象相关的Service
                .createDeployment()//创建部署对象
                .name(fileName)
                .addZipInputStream(zipInputStream)//使用zip方式部署，将leave.bpmn和leave.png压缩成zip格式的文件
                .deploy();//完成部署
        return CommonResult.success();
    }

    /**
     * 查询流程部署信息
     * @return
     * @throws ApiException
     */
    @PostMapping("/findProcessDeployList")
    public CommonResult<CommonPage<ActReProcdef>> findProcessDeployList(@RequestBody ActReProcdef vo) throws ApiException{
        IPage<ActReProcdef> pageList =  workFlowCommonService.findProcessDeployList(vo);
        return  CommonResult.success(CommonPage.restPage(pageList));
    }

    /**
     * 创建流程
     * @param vo
     * @return
     * @throws ApiException
     */
    @PostMapping("/createProcessInstance")
    public CommonResult<String> createProcessInstance(@RequestBody WorkFlowVO vo) throws ApiException{
        String processInstanceId = workFlowCommonService.createProcessInstance(vo);
        if(!StringUtils.isEmpty(processInstanceId)){
            return CommonResult.success(processInstanceId);
        }
        return CommonResult.failed();
    }

    /**
     * 获取流程信息
     * @param vo
     * @return
     * @throws ApiException
     */
    @PostMapping("/workFlowInfo")
    public CommonResult<WorkFlowVO> workFlowInfo(@RequestBody WorkFlowVO vo) throws ApiException{
        WorkFlowVO workFlowVO = workFlowCommonService.workFlowInfo(vo);
        if(workFlowVO!=null){
            return CommonResult.success(workFlowVO);
        }
        return CommonResult.failed();
    }

    @PostMapping("/findCurrUserToDoTasks")
    public CommonResult<CommonPage<WorkFlowVO>> findCurrUserToDoTasks(@RequestBody WorkFlowVO vo) throws ApiException{
        IPage<WorkFlowVO> list = workFlowCommonService.findCurrUserToDoTasks(vo);
        if(list!=null){
            return CommonResult.success(CommonPage.restPage(list));
        }
        return CommonResult.failed();
    }

    @PostMapping("/processWorkFlow")
    public CommonResult<WorkFlowVO> processWorkFlow(@RequestBody WorkFlowVO vo) throws ApiException{
        workFlowCommonService.processWorkFlow(vo);
        return CommonResult.success();
    }

    /**
     * 审批历史
     * @param vo
     * @return
     * @throws ApiException
     */
    @PostMapping("/historyWorkFlowList")
    public CommonResult<List<WorkFlowHiVO>> historyWorkFlowList(@RequestBody WorkFlowVO vo) throws ApiException{
        List<WorkFlowHiVO> list = workFlowCommonService.historyWorkFlowList(vo.getProcessInstanceId());
        return CommonResult.success(list);
    }

    /**
     * 已审批申请单
     * @param vo
     * @return
     * @throws ApiException
     */
    @PostMapping("/findHistoryApproval")
    public CommonResult<CommonPage<WorkFlowVO>> findHistoryApproval(@RequestBody WorkFlowVO vo) throws ApiException{
        IPage<WorkFlowVO> list = workFlowCommonService.findHistoryApproval(vo);
        return CommonResult.success(CommonPage.restPage(list));
    }




}
